//
//  HMNetworkTools.swift
//  Swift网络工具类
//
//  Created by apple on 16/1/19.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import AFNetworking

/// 请求方式的枚举
///
/// - GET:  GET
/// - POST: POST
enum HMRequestMethod: String {
    case GET = "GET"
    case POST = "POST"
}

class HMNetworkTools: AFHTTPSessionManager {
    
    /// 全局访问点
    static let sharedTools: HMNetworkTools = {
        let tools = HMNetworkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    
    
    /// 发送请求
    ///
    /// - parameter urlString:  <#urlString description#>
    /// - parameter parameters: <#parameters description#>
    /// - parameter finished:   <#finished description#>
    func request(method: HMRequestMethod = .GET,urlString: String, parameters: AnyObject?, finished:(response: AnyObject?, error: NSError?)->()){
        
        // 因为 get 与 post 两个方法请求的参数一样,所以可以把参数提取出来共用
        // 其实就是提供请求成功与请求失败的闭包出来,然后在调用 方法的时候去传入闭包就可以了
        
        
        // 定义请求成功的闭包
        let success = {(dataTask: NSURLSessionDataTask, response: AnyObject?) -> Void in
            finished(response: response, error: nil)
        }
        
        // 定义请求失败的闭包
        let failure = {(dataTask: NSURLSessionDataTask?, error: NSError) -> Void in
            finished(response: nil, error: error)
        }
        
        // 根据请求方式,发送不同的请求
        if method == .GET {
            GET(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }else{
            POST(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
    }
}

// MARK: - 首页微博数据请求
extension HMNetworkTools {

    /// 请求首页数据
    ///
    /// - parameter accessToken: <#accessToken description#>
    /// - parameter finished:    <#finished description#>
    func loadStatuses(accessToken: String, finished:(response: AnyObject?, error: NSError?)->()){
        let urlString = "https://api.weibo.com/2/statuses/friends_timeline.json"
        let params = [
            "access_token": accessToken
        ]
        request(urlString: urlString, parameters: params, finished: finished)
    }
}



// MARK: - 登录获取AccessToken和个人信息
extension HMNetworkTools {
    
    /// 请求AccessToken
    ///
    /// - parameter code:     授权码
    /// - parameter finished: 请求完成回调
    func loadAccessToken(code: String, finished:(response: AnyObject?, error: NSError?)->()) {
        // 请求地址
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        // 请求参数
        let params = [
//            "client_id": WB_CLIENT_ID,
//            "client_secret": WB_CLIENT_SECRET,
//            "grant_type": "authorization_code",
//            "code": code,
//            "redirect_uri": WB_REDIRECT_URI
        ]
        
        request(.POST, urlString: urlString, parameters: params, finished: finished)
        
    }
    
    
    /// 加载个人信息
    ///
    /// - parameter accessToken: <#accessToken description#>
    /// - parameter uid:         <#uid description#>
    /// - parameter finished:    请求完成的闭包
    func loadUserInfo(accessToken: String, uid: String, finished:(response: AnyObject?, error: NSError?)->()){
        // 定义 url
        let urlString = "https://api.weibo.com/2/users/show.json"
        // 定义参数
        let params = [
            "access_token": accessToken,
            "uid": uid
        ]
        // 发送请求
        request(urlString: urlString, parameters: params, finished: finished)
    }
}


